
package com.yimixian.app.payment;

import com.alipay.sdk.app.PayTask;

import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

import android.app.Activity;

public class PaymentController {

    private static PaymentController sInstance;

    public static PaymentController getInstance() {
        if (sInstance == null) {
            sInstance = new PaymentController();
        }
        return sInstance;
    }

    public Observable<AlipayPayResult> payWithAlipay(final String orderInfo, final Activity caller) {
        return Observable
                .create(new Observable.OnSubscribe<AlipayPayResult>() {
                    @Override
                    public void call(Subscriber<? super AlipayPayResult> subscriber) {
                        if (subscriber.isUnsubscribed()) {
                            return;
                        }

                        PayTask payTask = new PayTask(caller);
                        subscriber.onNext(new AlipayPayResult(payTask.pay(orderInfo)));
                        subscriber.onCompleted();
                    }
                })
                .subscribeOn(Schedulers.newThread());
    }
}
